# -*- coding:utf-8 -*-

import re

txt_list = [
'CSCWD (Selected papers)',

'FSKD (2)',

'Web Intelligence/IAT Workshops',

'International Conference on Computational Science (2)',

'Proceedings of the 2009 WRI International Conference on Communications and Mobile Computing - Volume 01',


'Proceedings of the   Asian Test Symposium',
'The Journal of Machine Learning Research',
'Journal of Computational Biology',

'IEEE Trans. Vis. Comput. Graph.',
'IEEE Trans. on CAD of Integrated Circuits and Systems',
'IEEE Transactions on Audio, Speech & Language Processing',

'Workshop on Vision Algorithms',
'MoDELS Workshops',


'RoboCup Robot Soccer World Cup I',
'RoboCup Robot Soccer World Cup II',

'Computer Science R D',

'Zhejiang University Science C'
]

def get_tokens(sentence):
    return [tok.lower() for tok in re.findall(r"\w+", sentence)]
# url_pattern = ' - |\(|\)|[0-9]|\s+|\t+|/'


def replace_symbol_use_space(sentence, url_pattern):
    pattern_rule = re.compile(url_pattern)
    sentence = re.sub(pattern_rule, " ", sentence)
    return sentence.strip()

bracket_digit_url_pattern = '\([0-9]+\)'

# other_num_url_pattern = ' - |Volume [0-9]{2,}|Vol. [0-9]{1,}|[0-9](st|nd|rd|th)|[0-9]-[0-9]|[0-9]{4,}|Part [A-Z]'
# other_num_url_pattern = ' - |Volume [0-9]{2,}|Vol. [0-9]{1,}|[0-9](st|nd|rd|th)|[0-9]-[0-9]|[0-9]{4,}|Part [A-Z]'
other_num_url_pattern = ' - |-[0-9]+|[0-9]+-|[0-9]+-[0-9]+|Volume [0-9]{1,}|Vol. [0-9]{1,}|volume [0-9]{1,}|[0-9]+(st|nd|rd|th)|[0-9]{4,}|Part [A-Z]|part [A-Z]|case [0-9]{1,}|Case [0-9]{1,}'

roman_num_url_pattern = ' IX| IV| VI{0,3}| II| III| I$|[0-9]+'

# other_symbol_url_pattern = '\'|&|:|：|;| (a-z|A-Z)( |$)|\(|\)'

other_symbol_url_pattern = '\'|&|:|：|;|\(|\)|,|\.'

one_letter_url_pattern = '( |^)([a-z]|[A-Z])( |$)'
# url_pattern = ' ([a-z]|[A-Z])( |$)'

journal_trans_on_url_pattern = '(International |The |)(Journal|Trans.|Transactions) (on|of|)'

# journal_of_url_pattern = '(International |The |)Journal of'

# proceedings_url_pattern = 'Proceedings of (.+?) on'
proceedings_url_pattern = 'Proceedings of ((.+?) on|the |)'

workshop_url_pattern = 'Workshop on |Workshops'


space_url_pattern = '\s+|\t+'

def drop_symbol_digit(sentence):
    return replace_symbol_use_space(sentence, bracket_digit_url_pattern)

def drop_symbol_other_num(sentence):
    return replace_symbol_use_space(sentence, other_num_url_pattern)

def drop_symbol_roman_num(sentence):
    return replace_symbol_use_space(sentence, roman_num_url_pattern)


def drop_other_symbol(sentence):
    return replace_symbol_use_space(sentence, other_symbol_url_pattern)

def drop_proceedings(sentence):
    return replace_symbol_use_space(sentence, proceedings_url_pattern)


def drop_journal_trans_on_of(sentence):
    return replace_symbol_use_space(sentence, journal_trans_on_url_pattern)

# def drop_journal_of(sentence):
#     return replace_symbol_use_space(sentence, journal_of_url_pattern)

def drop_proceedings(sentence):
    return replace_symbol_use_space(sentence, proceedings_url_pattern)

def drop_workshop(sentence):
    return replace_symbol_use_space(sentence, workshop_url_pattern)

def drop_symbol_space(sentence):
    return replace_symbol_use_space(sentence, space_url_pattern)

def drop_one_letter(sentence):
    return replace_symbol_use_space(sentence, one_letter_url_pattern)

def clean_text_digit(sentence):
    flag = True
    while(flag):
        last_sen = sentence
        sentence = drop_symbol_digit(sentence)
        if last_sen == sentence:
            flag = False
    return sentence

def clean_text_other_num(sentence):
    flag = True
    while (flag):
        last_sen = sentence
        sentence = drop_symbol_other_num(sentence)
        if last_sen == sentence:
            flag = False
    return sentence


def clean_text_roman_num(sentence):
    flag = True
    while (flag):
        last_sen = sentence
        sentence = drop_symbol_roman_num(sentence)
        if last_sen == sentence:
            flag = False
    return sentence

def clean_text_other_symbol(sentence):
    flag = True
    while (flag):
        last_sen = sentence
        sentence = drop_other_symbol(sentence)
        if last_sen == sentence:
            flag = False
    return sentence

def clean_text_proceedings(sentence):
    flag = True
    while (flag):
        last_sen = sentence
        sentence = drop_proceedings(sentence)
        if last_sen == sentence:
            flag = False
    return sentence

def clean_text_journal_trans_on_of(sentence):
    flag = True
    while (flag):
        last_sen = sentence
        sentence = drop_journal_trans_on_of(sentence)
        if last_sen == sentence:
            flag = False
    return sentence

def clean_text_workshop(sentence):
    flag = True
    while (flag):
        last_sen = sentence
        sentence = drop_workshop(sentence)
        if last_sen == sentence:
            flag = False
    return sentence

def clean_text_one_letter(sentence):
    flag = True
    while (flag):
        last_sen = sentence
        sentence = drop_one_letter(sentence)
        if last_sen == sentence:
            flag = False
    return sentence

    # sentence = drop_journal_of(sentence)
def clean_text_space(sentence):
    flag = True
    while (flag):
        last_sen = sentence
        sentence = drop_symbol_space(sentence)
        if last_sen == sentence:
            flag = False
    return sentence


def clean_main_bak2(sentence):
    # sentence = drop_symbol_digit(sentence)
    sentence = clean_text_other_num(sentence)
    sentence = clean_text_roman_num(sentence)

    sentence = clean_text_proceedings(sentence)
    sentence = clean_text_journal_trans_on_of(sentence)
    sentence = clean_text_workshop(sentence)

    sentence = clean_text_other_symbol(sentence)
    sentence = clean_text_one_letter(sentence)
    sentence = clean_text_space(sentence)
    return sentence

def clean_main_bak(sentence):
    # sentence = drop_symbol_digit(sentence)
    sentence = drop_symbol_other_num(sentence)
    sentence = drop_symbol_roman_num(sentence)

    sentence = drop_proceedings(sentence)
    sentence = drop_journal_trans_on_of(sentence)
    sentence = drop_workshop(sentence)

    sentence = drop_one_letter(sentence)
    sentence = drop_other_symbol(sentence)
    sentence = drop_symbol_space(sentence)
    return sentence


def clean_main(sentence):
    sentence = clean_text_other_num(sentence)
    sentence = clean_text_roman_num(sentence)

    sentence = clean_text_other_symbol(sentence)
    sentence = clean_text_space(sentence)
    return sentence

def get_clean_paper_venue( paper_venue):
    if len(paper_venue.split(' ')) > 1:
        paper_venue = clean_text_digit(paper_venue)
        if len(paper_venue.split(' ')) > 1:
            paper_venue = clean_main(paper_venue)
            return paper_venue
        else:
            return paper_venue
    else:
        if paper_venue != 'null':
            return paper_venue
        else:
            return None

def get_clean_paper_venue_bak2(nltk_util, paper_venue): #
    if len(paper_venue.split(' ')) > 1:
        paper_venue = clean_text_digit(paper_venue)
        if len(paper_venue.split(' ')) > 1:
            paper_venue = clean_main_bak2(paper_venue)

            paper_venue = nltk_util.filter_stop_words(paper_venue)
            return paper_venue
        else:
            return paper_venue
    else:
        return paper_venue


"""
get_clean_paper_venue_list is not used
"""

def get_clean_paper_venue_list(paper_venue_list):
    venue_set = set()
    for venue in paper_venue_list:
        if len(venue.split(' ')) > 1:
            venue = clean_text_digit(venue)
            if len(venue.split(' ')) > 1:
                venue = clean_main(venue)
                venue_set.add(venue)
            else:
                venue_set.add(venue)
        else:
            venue_set.add(venue)

#
# for txt in txt_list:
#     print clean_text_digit(txt)

# txt = 'Computing - Volume'
# print clean_text(txt)

# for i, txt in enumerate(txt_list):
#     txt_list[i] = clean_text_other_num(txt_list[i] )


#
# print drop_proceedings( txt_list[5] )
#
# print drop_journal_of(txt_list[6])
# print drop_journal_of(txt_list[7])

# print drop_journal_trans_on(txt_list[8])
# print drop_journal_trans_on(txt_list[9])
# print drop_journal_trans_on(txt_list[10])
#
# print drop_workshop(txt_list[11])
# print drop_workshop(txt_list[12])

# print drop_symbol_roman_num(txt_list[13])
# print drop_symbol_roman_num(txt_list[14])

# txt = 'Computer Science R D'
#
# print drop_one_letter(txt)
# print drop_one_letter(txt)

# print clean_text_one_letter(txt)

# print get_clean_paper_venue(txt_list[4])